//list页面的控制器

angular.module('listModule.controller', ['listModule.service'])
	.controller('listController', ['$scope','$stateParams','listFactory',function($scope,$stateParams,listFactory) {
		$scope.type=$stateParams.type;
		$scope.loading=true;
		$scope.page={
			type:$stateParams.type,
			count:5,//每页显示的条数
			start:0,//从第几条开始请求
			current:1,//当前是第几页
			totalCount:0,//一共的总条数
			totalPage:0//总页数
		}


		// 下一页
		$scope.goForward=function(){
			if($scope.page.current >= $scope.page.totalPage)
			{
				return false;
			}
			$scope.page.current++
			$scope.getData();
		}

		// 上一页
		$scope.goBack=function(){

			if($scope.page.current<= 1)
			{
				return false;
			}

			$scope.page.current--;
			$scope.getData();
		}

		$scope.getData=function(){
			$scope.loading=true;
			listFactory.getData({count:5,start:$scope.page.count*($scope.page.current-1)},$scope.page.type,function(data){
				$scope.moviesList=data;
				$scope.loading=false;

				$scope.page.totalCount=data.total;
				$scope.page.totalPage=Math.ceil(data.total/$scope.page.count)
				$scope.$apply();

			});
		}

		$scope.searchData=function(search){
			$scope.loading=true;
			listFactory.searchData({q:search,count:5,start:$scope.page.count*($scope.page.current-1)},function(data){
				$scope.moviesList=data;
				$scope.loading=false;

				$scope.page.totalCount=data.total;
				$scope.page.totalPage=Math.ceil(data.total/$scope.page.count)
				$scope.$apply();

			});
		};

		$scope.getData();
		$scope.$emit("list-to-app",$scope.searchData)


	}])
